#include  "FlashMemory.h"
#include <hal_types.h>
#include <hal_defs.h>
#include <hal_cc8051.h>
#include <ioCCxx10_bitdef.h>

#include <ioCC2430.h>



// These values will give a baud rate of approx. 1.002930 Mbps for 26 MHz clock
#define SPI_BAUD_M  0
#define SPI_BAUD_E  17

// Define size of buffer and number of bytes to send


void  SPIInit(void);
void Wait_Busy(void);

void  SPIInit(void)
{
  /*   PERCFG = (PERCFG | PERCFG_U1CFG);

    P1SEL = (P1SEL & ~BIT4) | BIT5 | BIT6 | BIT7;
    P1DIR |= BIT4;

    // Set USART to SPI mode and Master mode
    U1CSR &= ~(U1CSR_MODE | U1CSR_SLAVE);

    U1BAUD = SPI_BAUD_M;
    U1GCR = (U1GCR & ~(U1GCR_BAUD_E | U1GCR_CPOL | U1GCR_CPHA | U1GCR_ORDER))
        | SPI_BAUD_E | U1GCR_ORDER;
    P1_4 = 1;*/

    PERCFG = (PERCFG & (~PERCFG_U0CFG));

    P0SEL = (P0SEL & ~BIT4) | BIT2 | BIT3 | BIT5;
    P0DIR |= BIT4;

    // Set USART to SPI mode and Master mode
    U0CSR &= ~(U0CSR_MODE | U0CSR_SLAVE);

    U0BAUD = SPI_BAUD_M;
    U0GCR = (U0GCR & ~(U0GCR_BAUD_E | U0GCR_CPOL | U0GCR_CPHA | U0GCR_ORDER))
        | SPI_BAUD_E | U0GCR_ORDER;
    P0_4 = 1;

}

uint8 Flash_Send_Byte(uint8 data)
{
    uint8 datareture=0;
    U0DBUF = data;
    while(!(U0CSR & U0CSR_TX_BYTE));
    U0CSR &= ~U0CSR_TX_BYTE;
    datareture=U0DBUF;
    return datareture;
}




void Wait_Busy(void)
{
	Flash_Select();
    Flash_Send_Byte(0xd7);

    while(1)
    {
    	if(Flash_Send_Byte(0x00) & 0x80)
            break;
    }

    Flash_UNSelect();
}


void Write_Datas_To_Buffer(uint8 Buffer_Area, uint16 Buffer_Addr, uint8* Datas_Addr, uint16 Data_Length)
{
	uint8 Buf_Area;
	uint16 i;
	
	if( Buffer_Area == 1)
		Buf_Area = 0x84;
	else
		Buf_Area = 0x87;
	
	Wait_Busy();
	Flash_Select();
	Flash_Send_Byte(Buf_Area);
	Flash_Send_Byte(0x00);
	Flash_Send_Byte( (uint8)(Buffer_Addr >> 8) );
	Flash_Send_Byte((uint8)Buffer_Addr);
	
	for(i = 0; i < Data_Length; i++)
	    Flash_Send_Byte( Datas_Addr[i] );	
	Flash_UNSelect();	
}


void Write_Buffer_To_Momery(uint8 Buffer_Area, uint16 Memory_Page)
{
	uint8 Buf_Area;
	
	if( Buffer_Area == 1)
		Buf_Area = 0x83;
	else
		Buf_Area = 0x86;
	
	Wait_Busy();
	Flash_Select();
	Flash_Send_Byte(Buf_Area);
	Flash_Send_Byte( (uint8)(Memory_Page >> 6) );
	Flash_Send_Byte( (uint8)(Memory_Page << 2) );
	Flash_Send_Byte( 0x00 );	
	Flash_UNSelect();	
}


void Read_Momery_To_Buffer(uint8 Buffer_Area, uint16 Memory_Page)
{
	uint8 Buf_Area;
	
	if( Buffer_Area == 1)
		Buf_Area = 0x53;
	else
		Buf_Area = 0x55;
	
	Wait_Busy();
	Flash_Select();
	Flash_Send_Byte(Buf_Area);
	Flash_Send_Byte( (uint8)(Memory_Page >> 6) );
	Flash_Send_Byte( (uint8)(Memory_Page << 2) );
	Flash_Send_Byte( 0x00 );	
	Flash_UNSelect();	
}


void Read_Buffer(uint8 Buffer_Area, uint16 Buffer_Addr, uint8* Datas_Addr, uint16 Data_Length)
{
	uint8 Buf_Area;
	uint16 i;

	if( Buffer_Area == 1)
		Buf_Area = 0xd4;
	else
		Buf_Area = 0xd6;
	
	Wait_Busy();
	Flash_Select();
	Flash_Send_Byte(Buf_Area);
	Flash_Send_Byte(0x00);
	Flash_Send_Byte( (uint8)(Buffer_Addr >> 8) );
	Flash_Send_Byte((uint8)Buffer_Addr);
	
	Flash_Send_Byte( 0x00 );	
	for(i = 0; i < Data_Length; i++)
	    Datas_Addr[i] = Flash_Send_Byte( 0x00 );	
	Flash_UNSelect();	
}